<template>
  <div class="login-container">
    <div class="login-content">
      <div class="login-box">
        <div class="login-header">
          <h2>用户登录</h2>
          <p>欢迎来到河南省体育与艺术用品招标采购平台</p>
        </div>
        <el-form :model="loginForm" :rules="rules" ref="loginFormRef" label-width="0px">
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              placeholder="用户名/手机号/邮箱"
              prefix-icon="el-icon-user"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              type="password"
              v-model="loginForm.password"
              placeholder="请输入密码"
              prefix-icon="el-icon-lock"
              show-password
              autocomplete="new-password"
            ></el-input>
          </el-form-item>
          <el-form-item prop="captcha">
            <div class="captcha-container">
              <el-input
                v-model="loginForm.captcha"
                placeholder="请输入验证码"
                prefix-icon="el-icon-key"
              ></el-input>
              <div class="captcha-image">
                <img src="../assets/captcha.svg" alt="验证码" @click="refreshCaptcha" />
              </div>
            </div>
          </el-form-item>
          <el-form-item>
            <div class="remember-forgot">
              <el-checkbox v-model="loginForm.remember">记住密码</el-checkbox>
              <router-link to="/forgot-password" class="forgot-link">忘记密码？</router-link>
            </div>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" class="login-button" @click="handleLogin">登录</el-button>
          </el-form-item>
          <el-form-item>
            <div class="register-link">
              还没有账号？<router-link to="/register">立即注册</router-link>
            </div>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { useUserStore } from '@/stores/user'

defineOptions({
  name: 'LoginView'
})

const router = useRouter()

// 表单数据
const loginForm = ref({
  username: '',
  password: '',
  captcha: '',
  remember: false
})

// 表单引用
const loginFormRef = ref(null)

// 表单验证规则
const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
  ],
  captcha: [
    { required: true, message: '请输入验证码', trigger: 'blur' },
    { min: 4, max: 4, message: '验证码为 4 位字符', trigger: 'blur' }
  ]
}

// 登录处理
const userStore = useUserStore()

const handleLogin = () => {
  if (!loginFormRef.value) return

  loginFormRef.value.validate((valid) => {
    if (valid) {
      userStore.setUser(loginForm.value.username)
      ElMessage.success('登录成功')
      router.push('/')
    }
  })
}

// 刷新验证码
const refreshCaptcha = () => {
  console.log('Refreshing captcha')
}
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.login-content {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 40px 0;
  background-color: #f5f7fa;
}

.login-box {
  width: 400px;
  padding: 30px;
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-header h2 {
  font-size: 24px;
  color: #333;
  margin-bottom: 10px;
}

.login-header p {
  font-size: 14px;
  color: #999;
}

.captcha-container {
  display: flex;
  gap: 10px;
}

.captcha-image {
  flex-shrink: 0;
  width: 120px;
  height: 40px;
  overflow: hidden;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  cursor: pointer;
}

.captcha-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.remember-forgot {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.forgot-link {
  color: #409eff;
  text-decoration: none;
  font-size: 14px;
}

.login-button {
  width: 100%;
}

.register-link {
  text-align: center;
  font-size: 14px;
  color: #666;
}

.register-link a {
  color: #409eff;
  text-decoration: none;
}
</style>
